import requests
import re

def get_one_page(url):    #得到第一页的html代码
    html = requests.get(url).text
    #print(html)

def get_info(html):    #正则得到所有需要的信息
    pattern = re.compile('class="board-index.*?">(.*?)</i>.*?title="(.*?)".*?data-src="(.*?)".*?class="star">(.*?)</p>.*?class="releasetime">(.*?)</p>.*?class="integer">(.*?)</i>.*?class="fraction">(.*?)</i>', re.S)
    items = re.findall(pattern, html)
    #print(items)
    for item in items:
        yield {            #创建生成器  生成字典
            'index':item[0],
            'name':item[1],
            'jpg_url':item[2],
            'actor':item[3].strip(),
            'time':item[4],
            'score':item[5]+item[6]
        }

def write_txt(item):
    with open('result.txt', 'a') as f:
        f.write(item)
        f.write('\n')
        f.close()


def main():
    for i in range(10):
        url = 'https://maoyan.com/board/4?offset=' + str(i*10)
        get_one_page(url)
        html = requests.get(url).text
        for item in get_info(html):
            print(item)
            write_txt(str(item))

if __name__ == '__main__':
    main()